package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        setmealDTO.setId(setmeal.getId());
        setmealDishMapper.insert(setmealDTO);
    }

    @Override
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> list = setmealMapper.selectByPage(setmealPageQueryDTO);
        Page<Setmeal> page = (Page<Setmeal>) list;


        return new PageResult(page.getTotal(), list);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        setmealMapper.deleteByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealIds(new Integer[]{setmealDTO.getId().intValue()});
        setmealDishMapper.insert(setmealDTO);

    }

    @Override
    public SetmealVO selectByIdWithDish(Integer id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(new Long(id.longValue()));
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> selectByCategoryId(Integer categoryId) {
        Setmeal setmeal = Setmeal.builder().categoryId(categoryId.longValue())
                .status(StatusConstant.ENABLE).build();
        List<Setmeal> list = setmealMapper.selectByCategoryId(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> selectDishItemVOBySetmealId(Integer id) {
        List<DishItemVO> list = setmealDishMapper.selectDishItemVOBySetmealId(id);
        return list;
    }


}
